using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using ResaGest.Metier.Entite.Hotel;
using ResaGest.DAO;
using ResaGest.Metier.Critere;

namespace ResaGest.DAO
{
    class hotelDAO
    {

		public static List<Hotel> getAllHotelByCritere(HotelCritere critere)
		{
			List<Hotel> lc = new List<Hotel>();
			string sqlCritere = "";
			if (critere.Type_hotel_id!= 0)
			{
				sqlCritere += "type_hotel_id = " + critere.Type_hotel_id + " and ";
			}
			if (!string.IsNullOrEmpty(critere.Pays))
			{
				sqlCritere += " pays_hotel like '" + critere.Pays.Replace("'", "''") + "' and ";
			}
			if (!string.IsNullOrEmpty(critere.Ville))
			{
				sqlCritere += "ville_hotel like'" + critere.Ville.Replace("'", "''") + "' and ";
			}
			if (!string.IsNullOrEmpty(critere.Nom))
			{
				sqlCritere += "nom_hotel like'" + critere.Nom.Replace("'", "''") + "' and ";
			}

			string requete = "";
			requete += sqlCritere != "" ? "select * from Hotel where " + sqlCritere.Substring(0, sqlCritere.LastIndexOf("and")) : "select * from Hotel";


			requSQL requ = new requSQL(requete);
			List<object> list = requ.getList();
			DataRow dr;
			Hotel ht = new Hotel();
			for (int i = 0; i < list.Count; i++)
			{
				dr = (DataRow)list[i];
				ht.Adresse =dr["adresse_hotel"] == System.DBNull.Value ? "":(string) dr["adresse_hotel"];
				ht.Id = (int)dr["id_hotel"];
				ht.Nom = (string)dr["nom_hotel"];
				ht.Pays = (string)dr["pays_hotel"];
				ht.Type_hotel_id = dr["type_hotel_id"] == System.DBNull.Value ? 0 : (int)dr["type_hotel_id"];
				ht.Ville = (string)dr["ville_hotel"];
				lc.Add(ht);
				ht = new Hotel();
			}


			return lc;
		}

		public static Hotel getHotelById(int id)
		{
			requSQL requ = new requSQL("select * from Hotel where id = "+id);
			List<object> list = requ.getList();
			DataRow dr;
			Hotel ht = new Hotel();
			dr = (DataRow)list[0];
			ht.Adresse = (string)dr["adresse_hotel"];
			ht.Id = (int)dr["id_hotel"];
			ht.Nom = (string)dr["nom_hotel"];
			ht.Pays = (string)dr["pays_hotel"];
			ht.Type_hotel_id =(int) dr["type_hotel_id"];
			ht.Ville = (string)dr["ville_hotel"];
			return ht;
		}
    }
}
